<?php

namespace App\Service\OpenShop\Feishu;


use App\Models\OpenShop\Common\OpenShopOrderManagerModel;
use App\Remote\Rms\RmsUserRemote;
use App\Service\BaseService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Collection;

/**
 * @author Green Leaf <786407418@qq.com>
 */
class OrderManagerService extends BaseService
{


    public static ?Collection $remoteRmsUser = null;
    public static ?Collection $orderManager = null;

    /**
     * @throws GuzzleException
     */
    public static function getOrderManagerInfo(array $mainProcessIds, int $mainProcessId = 0, int $type = 0): array
    {
        if (self::$remoteRmsUser === null) {
            self::$orderManager = OpenShopOrderManagerModel::query()
                                                           ->whereIn("mainProcessId", $mainProcessIds)
                                                           ->when($type, function ($query) use ($type) {
                                                               $query->where('type', $type);
                                                           })
                                                           ->where("orderStatus",
                                                               OpenShopOrderManagerModel::ORDER_STATUS_NORMAL)
                                                           ->where("isDeleted", OpenShopOrderManagerModel::NORMAL)
                                                           ->get();

            if($orderManagerUids =  self::$orderManager->pluck("managerUid")->unique()->toArray()){
                $remoteRmsUser = RmsUserRemote::getRmsUserByIds($orderManagerUids);
            }else{
                $remoteRmsUser = [];
            }
            self::$remoteRmsUser = collect($remoteRmsUser ?? [])->keyBy("id");
        }

        $remoteRmsUserCollection = self::$remoteRmsUser;
        if ($mainProcessId > 0) {
            return self::$orderManager->where("mainProcessId", $mainProcessId)->map(function ($v) use (
                $remoteRmsUserCollection
            ) {
                if (
                    !$remoteRmsUserCollection->offsetExists($v->managerUid) ||
                    $remoteRmsUserCollection->get($v->managerUid)['isDeleted'] != 0 ||
                    $remoteRmsUserCollection->get($v->managerUid)['status'] != 0
                ) {
                    return $v->only(["id", "managerName", "managerUid", "description"]);
                }
            })->filter()->values()->toArray();
        } else {
            return self::$orderManager->map(function ($v) use ($remoteRmsUserCollection) {
                $v->rmsUser = (object)($remoteRmsUserCollection[$v->managerUid] ?? []);
                return $v;
            })->toArray();
        }
    }


    /**
     * 督导关联门店检验
     * @param array $list
     * @return array
     */
    public static function checkManagerShop(array $list): array
    {
        $checkShopDatas = [];
        foreach ($list as $v) {
            $checkShopData = OpenShopOrderManagerModel::getNotRelShopIdByUid($v['shopIds'], [$v['userId']]);
            if ($checkShopData) {
                $checkShopDatas = array_merge($checkShopDatas, $checkShopData);
            }
        }
        return $checkShopDatas;
    }
}
